package com.ykm.testapp;

import android.content.Context;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.ykm.testapp.bean.Dog;
import com.ykm.testapp.bean.People;
import com.ykm.testapp.bean.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.realm.Realm;
import io.realm.RealmChangeListener;
import io.realm.RealmList;
import io.realm.RealmResults;

/**
 * Created by ykm on 2017/10/9.
 */

public class Test {
    private static final String TAG = "Test";
    private static Test instance;
    private static Context context;

    public static synchronized Test getInstance() {
        if (instance == null) {
            instance = new Test(TestApp.getInstance());
        }
        return instance;
    }

    private Test(Context context) {
        this.context = context;
    }

    public void testlog() {
        Logger.wtf("wtf");
        Logger.w("warn");
        Logger.i("Logger.i");
        Logger.e("Logger.e");
        Logger.d("Logger.d");
        String XML_CONTENT = "<china dn=\"nay\"><city quName=\"黑龙江\" pyName=\"heilongjiang\" " +
                "cityname=\"哈尔滨\" state1=\"1\" state2=\"1\" stateDetailed=\"多云\"/><city quName=\"吉林\"" +
                " pyName=\"jilin\" " +
                "cityname=\"长春\" state1=\"0\" state2=\"0\" stateDetailed=\"晴\"/><city quName=\"辽宁\" " +
                "pyName=\"liaoning\" " +
                "cityname=\"沈阳\" state1=\"1\" state2=\"0\" stateDetailed=\"多云转晴\"/><city " +
                "quName=\"海南\" pyName=\"hainan\" " +
                "cityname=\"海口\" state1=\"22\" state2=\"21\" stateDetailed=\"中到大雨转小到中雨\"/></china>";
//输出上面的xml格式的数据
        Logger.xml(XML_CONTENT);
        String JSON_CONTENT = "{\"weatherinfo\":{\"city\":\"北京\",\"cityid\":\"101010100\"," +
                "\"temp\":\"18\",\"WD\":\"东南风\",\"WS\":\"1级\",\"SD\":\"17%\",\"WSE\":\"1\"," +
                "\"time\":\"17:05\",\"isRadar\":\"1\",\"Radar\":\"JC_RADAR_AZ9010_JB\"," +
                "\"njd\":\"暂无实况\",\"qy\":\"1011\",\"rain\":\"0\"}}";
//输出上面的json格式的数据
        Logger.json(JSON_CONTENT);
        List<String> strList = new ArrayList<>();
        strList.add("list 1");
        strList.add("list 2");
        strList.add("list 3");
//输出上面List数据
        Logger.d(strList);
        Map<String, String> map = new HashMap<>();
        map.put("key_hello", "hello");
        map.put("key_world", "world");
//输出上面Map数据
        Logger.d(map);
//字符串格式化
        Logger.d("hello %s %d", "world", 5);
//使用t()设置临时Log名，临时的 TAG 名被追加到自定义 TAG 的后面了
        Logger.t(TAG).d("hello world");

        int[] num = new int[3];
        try {
            num[4] = 5;
        } catch (Exception e) {
            Logger.e(e, "错误信息");
        }
    }

    public static final String key = "key";

    public void testHawk() {
        Hawk.put(key, "va"); // Returns the result as boolean
        Hawk.put("dfas", "adsf"); // Returns the result as boolean
        Logger.d(Hawk.get(key));
        Logger.d(Hawk.count());
    }

    public void realmAdd() {
        final Realm realm = Realm.getDefaultInstance();
        //新增
        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                User user = realm.createObject(User.class);
                user.setName("jack");
                user.setAge(21);

                Dog dog1 = realm.createObject(Dog.class);
                dog1.setName("二哈");
                dog1.setAge(1);
                user.getDogs().add(dog1);

                Dog dog2 = realm.createObject(Dog.class);
                dog2.setName("阿斯蒂芬");
                dog2.setAge(1);
                user.getDogs().add(dog2);
            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
                Logger.d("user count: %d", realm.where(User.class).count());
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                Logger.d("onError： %s", error.getMessage());
            }
        });

        // 有主键的新增
        final People people = new People();
        people.setName("name");
        people.setDogs(new RealmList<Dog>());
        people.getDogs().add(new Dog());
        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                realm.copyToRealmOrUpdate(people);
            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
                Logger.d("people count: %d", realm.where(People.class).count());
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                Logger.d("onError： %s", error.getMessage());
            }
        });
    }

    public void realmWhere() {
        Realm realm = Realm.getDefaultInstance();
        //查询
        Logger.d("dog count: %d", realm.where(Dog.class).count());
        // 异步查询，注册监听
        RealmResults<User> users = realm.where(User.class)
                .equalTo("dogs.name", "阿斯蒂芬")
                .or().equalTo("name", "jack")
                .findAllAsync();
        users.addChangeListener(new RealmChangeListener<RealmResults<User>>() {
            @Override
            public void onChange(RealmResults<User> element) {
                Logger.d(element);
                // 聚合方法
                long sum = element.sum("age").longValue();
                Logger.d("user age sum :  %d", sum);
            }
        });
    }

    public void deleteFromRealm() {
        //先查找到数据
        Realm realm = Realm.getDefaultInstance();
        final RealmResults<User> userList = realm.where(User.class).findAll();
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
//                userList.get(0).deleteFromRealm();
                userList.deleteFirstFromRealm();
            }
        });
    }

    public void changeRealm() {
        // 改
        Realm.getDefaultInstance().executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                realm.where(User.class).findFirst().setName("mike");
            }
        });
    }
}
